import json
import traceback
from gevent import monkey
import time

import inform
import query_data
from flask import Flask, jsonify
# from gevent import pywsgi
from gevent.pywsgi import WSGIServer
# 导入genvent猴子补丁， 实现并发操作
from concurrent.futures import ThreadPoolExecutor

# 下三行代码提供了网络服务多线程


monkey.patch_all()
app = Flask(__name__)


@app.route('/personal_income_tax/<message>')
def avs_info(message):

    taske = executor.submit(query_data.getfile, message)
    y = taske.result()
    return jsonify(y)
    # json数据直接返回
    # return y

    # 非json数据转换使用
    # return jsonify(y)


@app.route('/query/<message>')
def dq(message):
    taske = executor.submit(inform.q, message)
    res = taske.result()
    return str(res)


@app.route('/update/<message>')
def upd(message):
    taske = executor.submit(inform.times, message)
    res = taske.result()
    return str(res)


if __name__ == '__main__':
    executor = ThreadPoolExecutor(100)
    # json支持中文
    app.config['JSON_AS_ASCII'] = False
    # 可写为 WSGIServer(('0.0.0.0', 50009), app).serve_forever()
    # 提供高并发服务支持
    server = WSGIServer(('0.0.0.0', 50009), app)
    server.serve_forever()
